<template>
  <div>
    <!--面包屑-->
    <breadcrumb></breadcrumb>
    <!-- 卡片-->
    <el-card class="box-card">
      <el-row :gutter="20">
        <search :list="postList" :type="'post'" @clearSearch="clearSearch" @searchData="searchData"></search>
      </el-row>
      <!-- 用户列表-->
      <el-table :data="dataList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="publisher" label="发布者"></el-table-column>
        <el-table-column prop="title" label="标题" style="font-size:12px;"></el-table-column>
        <el-table-column prop="readCount" label="阅读量"></el-table-column>
        <el-table-column prop="praiseCount" label="点赞量"></el-table-column>
        <el-table-column prop="commentCount" label="评论量"></el-table-column>
        <el-table-column prop="pubDate" label="发布时间"></el-table-column>
        <el-table-column prop="collegeName" label="所属部门"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!--查看按钮-->
            <el-button
              type="primary"
              icon="el-icon-view"
              size="mini"
              @click="lookup(scope.row._id)"
            ></el-button>
            <!--改变类型-->
            <el-button
              type="success"
              icon="el-icon-s-flag"
              size="mini"
              @click="openTypeDialog(scope.row)"
            ></el-button>
            <!--删除按钮-->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deletes(scope.row._id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="改变类型" :visible.sync="typeChangeDialog" width="50%">
      <!--内容主体-->
      <el-form label-width="80px" status-icon>
        <el-form-item label="帖子类型:">
          <el-select v-model="typePost.type" placeholder="请选择" style="width:80%;">
            <el-option label="精华帖" value="1"></el-option>
            <el-option label="普通帖" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <!--按钮-->
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="typeChange()" :disabled="isChange">确定</el-button>
        <el-button @click="typeChangeDialog=false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import breadcrumb from '../breadcrumb.vue'
import search from '../search.vue'
export default {
  data() {
    return {
      postList: [],
      dataList: [],
      scope: '',
      typeChangeDialog: false,
      typePost: {
        _id: '',
        type: ''
      },
      otype: '',
      isChange: true // 修改类型中确定按钮
    }
  },
  created() {
    var path = this.$route.path
    var type
    if (path === '/suphome/ordinPost') {
      type = '0'
    } else if (path === '/suphome/creamPost') {
      type = '1'
    }
    this.getpostinfo(type).then(async data => {
      await this.comm.getcollege(this).then(res => {
        data.forEach(dataItem => {
          res.forEach(resItem => {
            if (dataItem.collegeId === resItem.collegeId) {
              dataItem.collegeName = resItem.collegeName
            }
          })
        })
      })
      this.postList = data
      this.dataList = data
    })
  },
  methods: {
    // 获取帖子
    async getpostinfo(type) {
      const res = await this.$http.get('/postinfo/' + type)
      if (res.meta.status !== 200) {
        this.$Message.error('服务器请求失败')
      }
      return res.data
    },
    searchData(data) {
      this.dataList = data
    },
    clearSearch() {
      this.dataList = this.postList
    },
    // 查看
    lookup(scope) {
      this.$router.push({ path: '/suphome/showPost', query: { id: scope } })
    },
    // 删除
    deletes(scope) {
      this.comm.deletes(scope, '/postinfo/', this).then(() => {
        this.comm.ArrSplice(this.dataList, '_id', scope)
      })
    },
    // 改变类型对话框
    openTypeDialog(scope) {
      this.typeChangeDialog = true
      this.typePost._id = scope._id
      this.otype = scope.type
    },
    // 提交改变类型
    async typeChange() {
      const res = await this.$http.post('typeChange', this.typePost)
      if (res.meta.status !== 200) {
        this.$Message.error('修改类型失败')
      }
      this.typeChangeDialog = false
      this.comm.ArrSplice(this.dataList, '_id', this.typePost._id)
    }
  },
  components: {
    breadcrumb,
    search
  },
  watch: {
    'typePost.type': function(val) {
      if (val !== this.otype) {
        this.isChange = false
      } else {
        this.isChange = true
      }
    },
    '$route.path': function(val) {
      console.log('a')
      this.$router.go(0)
    }
  }
}
</script>
<style lang="less" scoped>
.el-breadcrumb {
  margin-bottom: 10px;
}
.el-table {
  margin-top: 15px;
}
.dialog-footer {
  display: flex;
  justify-items: center;
  .el-button {
    margin: 0 auto;
  }
}
</style>
